package com.yunke.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yunke.common.result.YunkeResult;
import com.yunke.entity.vo.YunkeConfigVo;
import com.yunke.service.YunkeConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统配置用户端控制器
 * 
 * @author yunke
 * @since 1.0.0
 */
@RestController
@RequestMapping("/config")
@Tag(name = "系统配置管理", description = "系统配置相关接口")
public class YunkeConfigController {

    @Autowired
    private YunkeConfigService configService;

    @GetMapping("/page")
    @Operation(summary = "分页查询配置列表")
    @SaCheckLogin
    public YunkeResult<IPage<YunkeConfigVo>> getConfigPage(
            @Parameter(description = "当前页码") @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "每页条数") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "配置分类") @RequestParam(required = false) String configCategory,
            @Parameter(description = "配置组") @RequestParam(required = false) String configGroup,
            @Parameter(description = "配置名称") @RequestParam(required = false) String configName,
            @Parameter(description = "是否系统配置") @RequestParam(required = false) Boolean isSystem,
            @Parameter(description = "是否启用") @RequestParam(required = false) Boolean isEnabled) {
        
        IPage<YunkeConfigVo> result = configService.getConfigPage(current, size, 
            configCategory, configGroup, configName, isSystem, isEnabled);
        return YunkeResult.success(result);
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询配置详情")
    @SaCheckLogin
    public YunkeResult<YunkeConfigVo> getConfigById(
            @Parameter(description = "配置ID") @PathVariable Long id) {
        
        YunkeConfigVo config = configService.getConfigById(id);
        return YunkeResult.success(config);
    }

    @GetMapping("/key/{configKey}")
    @Operation(summary = "根据配置键名查询配置")
    @SaCheckLogin
    public YunkeResult<YunkeConfigVo> getConfigByKey(
            @Parameter(description = "配置键名") @PathVariable String configKey) {
        
        YunkeConfigVo config = configService.getConfigByKey(configKey);
        return YunkeResult.success(config);
    }

    @GetMapping("/category/{configCategory}")
    @Operation(summary = "根据分类查询配置列表")
    @SaCheckLogin
    public YunkeResult<List<YunkeConfigVo>> getConfigByCategory(
            @Parameter(description = "配置分类") @PathVariable String configCategory,
            @Parameter(description = "是否启用") @RequestParam(required = false) Boolean isEnabled) {
        
        List<YunkeConfigVo> configs = configService.getConfigByCategory(configCategory, isEnabled);
        return YunkeResult.success(configs);
    }

    @GetMapping("/group/{configGroup}")
    @Operation(summary = "根据配置组查询配置列表")
    @SaCheckLogin
    public YunkeResult<List<YunkeConfigVo>> getConfigByGroup(
            @Parameter(description = "配置组") @PathVariable String configGroup,
            @Parameter(description = "是否启用") @RequestParam(required = false) Boolean isEnabled) {
        
        List<YunkeConfigVo> configs = configService.getConfigByGroup(configGroup, isEnabled);
        return YunkeResult.success(configs);
    }

    @GetMapping("/user-visible")
    @Operation(summary = "查询用户可见配置列表")
    @SaCheckLogin
    public YunkeResult<List<YunkeConfigVo>> getUserVisibleConfigs(
            @Parameter(description = "是否启用") @RequestParam(required = false) Boolean isEnabled) {
        
        List<YunkeConfigVo> configs = configService.getUserVisibleConfigs(isEnabled);
        return YunkeResult.success(configs);
    }

    @GetMapping("/categories")
    @Operation(summary = "查询配置分类列表")
    @SaCheckLogin
    public YunkeResult<List<String>> getConfigCategories() {
        List<String> categories = configService.getConfigCategories();
        return YunkeResult.success(categories);
    }

    @GetMapping("/groups")
    @Operation(summary = "查询配置组列表")
    @SaCheckLogin
    public YunkeResult<List<String>> getConfigGroups(
            @Parameter(description = "配置分类") @RequestParam(required = false) String configCategory) {
        
        List<String> groups = configService.getConfigGroups(configCategory);
        return YunkeResult.success(groups);
    }

    @GetMapping("/value/{configKey}")
    @Operation(summary = "获取配置值")
    public YunkeResult<String> getConfigValue(
            @Parameter(description = "配置键名") @PathVariable String configKey,
            @Parameter(description = "默认值") @RequestParam(required = false) String defaultValue) {
        
        String value = configService.getConfigValue(configKey, defaultValue);
        return YunkeResult.success(value);
    }

    @GetMapping("/count")
    @Operation(summary = "统计配置数量")
    @SaCheckLogin
    public YunkeResult<Long> countConfigs(
            @Parameter(description = "配置分类") @RequestParam(required = false) String configCategory,
            @Parameter(description = "是否系统配置") @RequestParam(required = false) Boolean isSystem,
            @Parameter(description = "是否启用") @RequestParam(required = false) Boolean isEnabled) {
        
        long count = configService.countConfigs(configCategory, isSystem, isEnabled);
        return YunkeResult.success(count);
    }
} 